<template>
	<!-- 编辑评分结果表 -->
	<view>
		<u-form class="form" :model="form" ref="uForm" label-width="180">
			<u-form-item label="现场评审时间" prop="evaluateTime">
				<u-input v-model="form.evaluateTime" @click="showTime=true" />
				<u-calendar v-model="showTime" mode="date" @change="changeTime"></u-calendar>
			</u-form-item>
			<u-form-item label="企业名称">
				<view class="">{{form.companyName}}</view>
			</u-form-item>
			<u-form-item label="评审员" prop="expertName">
				<u-input v-model="form.expertName" />
			</u-form-item>
			<u-form-item label="监督员" prop="supervisorName">
				<u-input v-model="form.supervisorName" />
			</u-form-item>

			<u-form-item label="文件资料情况" prop="results" label-position="top">
				<view class="results">
					<view class="checkall">
						<u-checkbox size="48" v-model="iScheckedAll" :disabled="disabled" @change="checkedAll">全选</u-checkbox>
					</view>
					<view class="cb-ul">
						<u-checkbox-group width="50%">
							<u-checkbox v-model="item.checked" @change="boxChange" v-for="(item, index) in resultsOptions"
								:key="index" :name="item.dictValue" :disabled="disabled">
								<view class="cb-li">{{item.dictLabel}}</view>
							</u-checkbox>
						</u-checkbox-group>
					</view>
				</view>
			</u-form-item>

			<u-form-item label="总结" prop="content1" label-position="top">
				<u-input type="textarea" v-model="form.content1" />
			</u-form-item>
			<u-form-item label="前厅及后厨情况" prop="content2" label-position="top">
				<u-input type="textarea" v-model="form.content2" />
			</u-form-item>
		</u-form>

		<u-button type="primary" @click="submit" :disabled="disabled" :loading="loading">提交</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			let form = {
				id: null,
				evaluateId: null,
				companyId: null,
				year: null,
				evaluateTime: '',
				companyName: '',
				expertId: vk.getVuex('$user.userInfo.userId'),
				expertName: vk.getVuex('$user.userInfo.nickName'),
				supervisorName: '',
				results: '',
				content1: '',
				content2: ''
			}
			return {
				disabled: false, // 提交中
				loading: false, // 提交中

				evaluateId: '',

				showTime: false,
				resultsOptions: [],
				iScheckedAll: false,

				form,
				rules: {

				}
			}
		},
		onLoad(options) {
			this.initPage(options)
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			changeTime(e) {
				this.form.evaluateTime = e.result;
			},
			initPage(options = {}) {
				const eventChannel = this.getOpenerEventChannel();
				// 监听data事件，获取上一页面通过eventChannel.emit传送到当前页面的数据
				if (eventChannel.on) {
					eventChannel.on('data', (data) => {
						console.log('initPage', data)
						this.getData(data);
					});
				}

				// if(options.evaluateId) this.evaluateId = options.evaluateId;
				// else{
				// 	this.disabled = true;
				// 	return false;
				// }

				this.getDicts("score_file_result", 'resultsOptions');
				// if (this.evaluateId) this.getData();
			},
			// 根据字典类型查询字典数据信息
			getDicts(dictType, optName) {
				this.$util.request(this.$api.GetDicts + dictType)
					.then(res => {
						this[optName] = res.data
						
						this[optName] = this[optName].map(item => {
							return {...item, checked:false}
						})
					})
					.catch(err => {
						console.log('查询字典数据信息失败！', err);
					})

			},
			getData(options) {
				this.$util.request(this.$api.ResultsInfo + 'info', {
						evaluateId: options.evaluateId
					})
					.then(res => {
						console.log('评分结果详情 res', res);
						if(res.data){
							this.form = res.data
							
							let arr = this.form.results.split(',');
							this.resultsOptions.forEach(item => {
								item.checked = arr.indexOf(item.dictValue + '') >= 0
							})
						}else{
							this.form = {
								id: null,
								evaluateId: options.evaluateId,
								companyId: options.companyId,
								year: options.year,
								evaluateTime: vk.pubfn.timeFormat(new Date(),"yyyy-MM-dd"),
								companyName: options.companyName,
								expertId: vk.getVuex('$user.userInfo.userId'),
								expertName: vk.getVuex('$user.userInfo.nickName'),
								supervisorName: '',
								results: '',
								content1: '',
								content2: ''
							}
						}
					})
					.catch(err => {
						console.log('数据加载失败！', err);
						vk.alert('数据加载失败！')
						this.disabled = true;
					})
			},
			submit() {
				this.disabled = true;
				this.loading = true;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.submitForm();
					} else {
						this.disabled = false;
						this.loading = false;
					}
				});
			},
			submitForm() {
				let list = this.resultsOptions.filter(item => item.checked)
				let idList = vk.pubfn.arrayObjectGetArray(list, "dictValue");
				this.form.results = idList.toString()
				
				let method = this.form.id ? 'PUT' : 'POST';
				this.$util.request(this.$api.ResultsRestful, this.form, method)
					.then(res => {
						if (res.code === 200) {
							vk.toast("保存成功", "success", 1000, true, () => {
								const eventChannel = this.getOpenerEventChannel();
								if (eventChannel.emit) eventChannel.emit('refresh');
								vk.navigateBack();
							});
						} else {
							console.log('保存失败', res)
							this.disabled = false;
							this.loading = false;
							vk.alert(res.msg, '保存失败');
						}
					}).catch((err) => {
						console.log('保存异常', err)
						this.disabled = false;
						this.loading = false;
						vk.alert(JSON.stringify(err), '保存异常');
					})
			},
			checkedAll(e){
				this.resultsOptions.forEach(item => {
					item.checked = e.value
				})
			},
			boxChange(e){
				if(!e.value){
					this.iScheckedAll = false
				}
			},
		}
	}
</script>

<style>
	.form {
		margin: auto;
		width: 90%;
	}
	.results{
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
	}
	.checkall{
		margin-bottom: 40rpx;
	}
	.cb-ul{
		width: 100%;
	}
	.cb-li{
		/* width: 35vw; */
		/* width: 40%; */
		margin: 20rpx 0;
	}
</style>